<template>
  <div class="com-container">
    <div class="com-chart" ref="selectcourse"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: "SelectCourse",
  data() {
    return {
      mychart: null,
      data: null,
      allData: null, //服务器返回的数据
    };
  },
  created() {
    //在组件创建完成之后进行回调函数的注册
    this.$socket.registerCallBack('selectData',this.getData)
  },
  mounted() {
    this.initChart();
    //this.getData();
    //发送数据给服务器，对服务器请求数据
    this.$socket.send({
      action: 'getData',
      socketType: 'selectData',
      chartName: 'select_course',
      value: ''
    })
    window.addEventListener("resize", this.screenAdapter);
    //在页面加载完成的时候，主动进行屏幕的适配
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    //在组件销毁之后进行回调函数的取消
    this.$socket.unRegisterCallBack('selectData')
  },
  methods: {
    initChart() {
      this.mychart = this.$echarts.init(this.$refs.selectcourse, this.theme);
      const initOption = {
        legend: {
          top: "bottom",
          orient: "vertical",
          right: '8%',
          top: "20%",
          bottom: "20%",
        },
        title: {
          text: "▎线上课程参与情况",
          left: 20,
          top: 20,
        },
        grid: {
          top: "15%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true, //距离是包含坐标轴上的文字
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        series: [
          {
            type: "pie",
            name: "",
            roseType: "area",
            center: ["40%", "60%"],
            itemStyle: {
              borderRadius: 8,
            },
            label: {
                show: false
            },
            emphasis: {
                label: {
                    show: true
                }
            },
          },
        ],
      };
      this.mychart.setOption(initOption);
    },
    getData(ret) {
      //const { data: ret } = await this.$http.get("select_course");
      console.log(ret, "select_course");
      this.allData = ret;
      this.updateChart();
    },
    updateChart() {
      const data = this.allData;
      const legendData = data.map((item) => {
        return item.name;
      });
      const dataOption = {
        series: [{ data: data }],
        legend: {
          data: legendData,
        },
      };
      this.mychart.setOption(dataOption);
    },
    //当浏览器的大小发生变化时，调用该方法，完成屏幕的适配
    screenAdapter() {
      const titleFontSize = (this.$refs.selectcourse.offsetWidth / 100) * 3.6;
      const innerRadius = titleFontSize * 1.2;
      const outterRadius = innerRadius * 5;
      const adapterOption = {
        title: {
          textStyle: { fontSize: titleFontSize },
        },
        legend: {
          itemWidth: titleFontSize / 1.5,
          itemHeight: titleFontSize / 1.5,
          itemGap: titleFontSize / 1.5,
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            radius: [innerRadius, outterRadius],
            label: {
              textStyle: {
                fontSize: titleFontSize / 2,
              },
            },
          },
        ],
        tooltip: {
          textStyle: {
            fontSize: titleFontSize / 2,
          },
        },
      };
      this.mychart.setOption(adapterOption);
      //手动调用图表对象的resize
      this.mychart.resize();
    },
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.mychart.dispose()//销毁当前图表
      this.initChart()//重新以最新主题初始化图表对象
      this.screenAdapter()
      this.updateChart()
    }
  },
};
</script>

<style>
</style>